# CMake minimum version
cmake_minimum_required (VERSION 3.2)

# ----------------------------------------------------------------------------------------------------------
# Project Infomation
project(freeWear)
enable_language(ASM)

# BOARD define
message("-- >> target borad selected : simulator")
add_compile_definitions(SIMULATOR)

message("-- >> macro defined : SIMULATOR")
add_compile_definitions(CFG_PLATFORM_SIMULATOR)
message("-- >> macro defined : CFG_PLATFORM_SIMULATOR")

add_compile_definitions(CFG_OS_FREERTOS)
message("-- >> macro defined : CFG_OS_FREERTOS")

add_compile_definitions(MR_CFG_DEVICE=1)
message("-- >> macro defined : MR_CFG_DEVICE")

add_compile_definitions(MR_CFG_DEBUG=1)
message("-- >> macro defined : MR_CFG_DEBUG")

add_compile_definitions(MR_CFG_SERIAL=1)
message("-- >> macro defined : MR_CFG_SERIAL")

add_compile_definitions(MR_CFG_LCD=1)
message("-- >> macro defined : MR_CFG_LCD")

add_compile_definitions(CFG_EASYLOG_ENABLED)
add_compile_definitions(projCOVERAGE_TEST)
add_compile_definitions(LV_CONF_INCLUDE_SIMPLE)
add_compile_definitions(LV_LVGL_H_INCLUDE_SIMPLE)
add_compile_definitions(CPG_HEAP_SIZE=1073741824)
add_compile_definitions(CPG_SUPPORT_FREERTOS_HEAP)
# add_compile_definitions(CPG_SUPPORT_TLSF_HEAP)

# ----------------------------------------------------------------------------------------------------------
# Reset output path
set (CMAKE_OUTPUT_PATH              ${CMAKE_BINARY_DIR}/out)
set (CMAKE_EXECUTABLE_PATH          ${CMAKE_OUTPUT_PATH}/binary)
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_EXECUTABLE_PATH}/lib)

#
execute_process(COMMAND sh tools/scripts/build_image.sh WORKING_DIRECTORY ../)

aux_source_directory(platform/target/simulator/src DIR_SRCS)
INCLUDE_DIRECTORIES (platform/target/simulator/inc)

INCLUDE_DIRECTORIES (configs)

# platform
add_subdirectory(platform/system)

# drivers
add_subdirectory(drivers/drv_lcd)
add_subdirectory(drivers/drv_serial)

# kernel
add_subdirectory(kernal/cmsis_freertos)

# lvgl
add_subdirectory(components/littlevgl/lvgl)

# lv_lib_qrcode
add_subdirectory(components/littlevgl/lv_lib_qrcode)

# lv_drivers
add_subdirectory(components/littlevgl/lv_drivers)

# rtc misc
add_subdirectory(components/rtc_misc)

# cmsis-rtos-v2
add_subdirectory(kernal/cmsis_rtos_v2)

# ams
add_subdirectory(framework/ams)

# mr-library
add_subdirectory(framework/mr-library)

# log
add_subdirectory(framework/log)

# messager
add_subdirectory(components/messager)

# tlsf
add_subdirectory(components/tlsf)

# hal
add_subdirectory(hal)

# cmsis_mem
add_subdirectory(kernal/cmsis_mem)

# trace service
add_subdirectory(services/track)

# track
add_subdirectory(components/analyze_gpx)

# # cpost
# add_subdirectory(components/cpost)

# cmsis_rtos_trace
if(CFG_SUPPORT_RTOS_TRACE)
    add_subdirectory(kernal/cmsis_rtos_trace)
endif()

# apps
add_subdirectory(app)

# compile parameters
set (CMAKE_C_COMPILER "gcc")
set (CMAKE_CXX_COMPILER "g++")
set (CMAKE_ASM_COMPILER "gcc")
set (CMAKE_C_AR "ar")
set (CMAKE_LINKER "ld")
set (CMAKE_OBJCOPY "objcopy")
set (CMAKE_OBJDUMP "objdump")
set (CMAKE_SIZE "size")

set (C_WARNING_FLAGS "-Wall -Wstrict-prototypes -Wmissing-prototypes -Wno-format-truncation -lrt -fsanitize=address -fno-omit-frame-pointer -fsanitize=leak")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${C_WARNING_FLAGS} -ggdb3 -O0 -DprojCOVERAGE_TEST=1 -D_WINDOWS_ -fsanitize=address -fno-omit-frame-pointer")
set (CMAKE_EXE_LINKER_FLAGS "-ggdb3 -O0 -pthread")

set(CMAKE_BUILD_TYPE Debug)

# 指定生成目标
add_executable(${PROJECT_NAME} ${DIR_SRCS})

set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "-O0")

# 指定生成.map文件的路径
set(MAP_FILE "${CMAKE_BINARY_DIR}/${PROJECT_NAME}.map")

# 设置链接器选项来生成.map文件
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-Wl,-Map=${MAP_FILE}")

# set(LD_SCRIPT_PATH "${CMAKE_SOURCE_DIR}/platform/target/simulator/ld/platform.ld")
# set_target_properties(${PROJECT_NAME}  PROPERTIES LINK_FLAGS "-T ${LD_SCRIPT_PATH}")

# 添加链接库
target_link_libraries(${PROJECT_NAME} kernal_rtos kernal_cmsis lvgl system framework_mr_library drv_serial)
add_dependencies(${PROJECT_NAME} kernal_rtos kernal_cmsis lvgl system framework_mr_library drv_serial)
